<?php
namespace Biz\model\trade;

use Doraemon\pockets\datebase\BaseDbModel;

class OrderRemark extends BaseDbModel
{
	const _CONFIG_FILE_ = 'db_shop';

	const _CONFIG_SELECT_ = 'db_trade';

	const _DATABASE_ = 'shop_trade';

	const _TABLE_NAME = 'order_remark';


	const REMARK_TYPE_BUSINESS = 1;
	const REMARK_TYPE_HHZ = 2;

	const REMARK_STATUS_DEL = 9;
	const REMARK_STATUS_NORMAL = 1;

	const HHZ_REMARK_TYPE_COMFIRM = 1;
	const HHZ_REMARK_TYPE_APPEND = 2;
	const HHZ_REMARK_TYPE_API_APPEND = 3;

	const HHZ_REMARK_MAPS = [
		self::HHZ_REMARK_TYPE_COMFIRM => '客服已核实信息',
		self::HHZ_REMARK_TYPE_APPEND => '客服追加备注',
		self::HHZ_REMARK_TYPE_API_APPEND => '商家追加备注',
	];

	public static function getRemarksByOrderNo($orderNo)
	{
		return self::select('*', [
			'order_no' => $orderNo,
			'remark_status' => self::REMARK_STATUS_NORMAL,
			'ORDER' => ['add_time' => 'DESC']
		]);
	}

	public static function updateRemark($orderNo, $remarkType, $actuonUid, $date)
	{
		if ($date) {
			$date['action_uid'] = $actuonUid;
			return self::update($date, [
				'order_no' => $orderNo,
				'remark_type' => $remarkType,
			]);
		}
	}

	public static function addRemark($orderNo, $remarkType, $actionUid, $data)
	{
		if ($remarkType == self::REMARK_TYPE_HHZ
			&&
			(
			!isset($data['hhz_remark_type'])
				||
				!in_array($data['hhz_remark_type'],array_keys(
					self::HHZ_REMARK_MAPS)
				))) {
			throw \Exception('请选择客服备注类型');
		}
		$data['order_no'] = $orderNo;
		$data['remark_type'] = $remarkType;
		$data['action_uid'] = $actionUid;
		$data['remark_status'] = self::REMARK_STATUS_NORMAL;
		self::insert($data);
		return self::id();
	}
}